package io.zt.dangyuan;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;

import io.zt.dangyuan.object.UserModel;

import com.umeng.analytics.MobclickAgent;

import org.xutils.ex.HttpException;

import java.util.HashMap;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by Administrator on 2015/7/6.
 */
public class LoginActivity extends Activity {

    @InjectView(R.id.et_send_title)
    EditText etSendTitle;
    @InjectView(R.id.et_send_content)
    EditText etSendContent;
    @InjectView(R.id.btn_login)
    TextView btnLogin;
    @InjectView(R.id.btn_cancel)
    TextView btnCancel;
    @InjectView(R.id.ic_back)
    ImageButton icback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_login);
        //注解框架
        ButterKnife.inject(this);


        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //http://42.96.170.0/tq/api/login.json?username=test&password=202cb962ac59075b964b07152d234b70

                boolean isSuccess = checkStr();
                if (isSuccess) {
                    HashMap<String,Object> map = new HashMap<String,Object>();
                    map.put("username", etSendTitle.getText().toString());
                    map.put("password", Utils.str2md5(etSendContent.getText().toString()));
//                    WebUtils.sendPost(LoginActivity.this,"login", map, LoginActivity.this, new WebUtils.OnWebListenr() {
                    WebUtils.sendPost(LoginActivity.this, "login", map, new WebUtils.OnWebListenr() {
                        @Override
                        public void onSuccess(boolean isSuccess, String msg, String data) {
                            if (isSuccess) {
                                Log.d("zxl", isSuccess + "    " + msg + "     " + data);
                                UserModel user = JSON.parseObject(
                                        data, UserModel.class);
                                Log.d("zxl", "    姓名:" + user.getUsername());

                                final GlobalDataApplication app = (GlobalDataApplication) getApplication();

                                app.setUserModel(user);

                                finish();
                                //dialog();
                            } else {
                                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onFailed(HttpException error, String msg) {
                            Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                        }
                    });


                }
            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //finish();


            }
        });
        icback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    //对话框
    /*protected void dialog() {
        //AlertDialog.Builder normalDialog=new AlertDialog.Builder(getApplicationContext());
        AlertDialog.Builder normalDia=new AlertDialog.Builder(LoginActivity.this);
        normalDia.setTitle("普通的对话框");
        normalDia.setMessage("检测到您的信息未完整");

        normalDia.setPositiveButton("去填写", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                startActivity(new Intent(LoginActivity.this, PersonalActivity2.class));
            }
        });
        normalDia.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                finish();
            }
        });
        normalDia.create().show();
    }*/
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
//        getMenuInflater().inflate(R.menu.menu_read_love, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }


    public Boolean checkStr() {
        String showStr = null;

        if (TextUtils.isEmpty(etSendTitle.getText())) {
            showStr = "用户名不能为空";

        } else if (TextUtils.isEmpty(etSendContent.getText())) {
            showStr = "密码不能为空";
        }

        if (showStr == null) {
            return true;

        } else {
            Toast.makeText(getApplicationContext(), showStr, Toast.LENGTH_SHORT).show();
            return false;
        }

    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }


}


